#!/bin/bash

. $(dirname $0)/functions

BASE=$BASEDIR/graph2
if [ -n "$MSYSTEM" ]; then
    # Avoid Windows drive letter issues in the path of DEF:vname=rrdfile under MSYS or MSYS2
    BUILD=graph2
else
    BUILD=$BUILDDIR/graph2
fi

function do_update {
  $RRDTOOL update ${BUILD}.rrd 1300000001:54.540500:0.000002 1300000041:53.633500:1.494203 1300000081:53.513700:2.983955 1300000121:53.428200:4.470899 1300000161:53.359700:5.955546 1300000201:53.291300:7.438231 1300000241:53.222800:8.919214 1300000281:53.171500:10.398531 1300000321:53.120100:11.876360 1300000361:53.068800:13.352798 || return 1
  ok "update 10"
  $RRDTOOL update ${BUILD}.rrd 1300000401:53.017400:14.827785 1300000441:52.966100:16.301486 1300000481:52.914800:17.773855 1300000521:52.880500:19.244938 1300000561:52.829200:20.714808 1300000601:52.795000:22.183371 1300000641:52.743600:23.650719 1300000681:52.709400:25.116902 1300000721:52.658100:26.581966 1300000761:52.623800:28.045876 || return 1
  ok "update 20"
  $RRDTOOL update ${BUILD}.rrd 1300000800:50:29.508655 1300000841:50:30.970281 1300000881:50:32.430871 1300000921:50:33.890319 1300000961:50:35.348744 1300001001:50:36.806097 1300001041:50:38.262274 1300001081:50:39.717393 1300001121:50:41.171430 1300001161:50:42.624338 || return 1
  ok "update 30"
  $RRDTOOL update ${BUILD}.rrd 1300001201:50:44.076360 1300001241:50:45.527372 1300001281:50:46.977267 1300001321:50:48.426163 1300001361:50:49.874048 1300001401:50:51.320802 1300001441:50:52.766544 1300001481:51.939300:54.211346 1300001521:51.905100:55.655114 1300001561:51.870800:57.097893 || return 1
  ok "update 40"
  $RRDTOOL update ${BUILD}.rrd 1300001601:51.836600:58.539579 1300001641:51.785300:59.980265 1300001681:51.751100:61.420010 1300001721:51.716800:62.858674 1300001761:51.682600:64.296280 1300001801:51.648400:65.732981 1300001841:51.614100:67.168635 1300001881:51.562800:68.603267 1300001921:51.545700:70.036945 1300001961:51.511500:71.469683 || return 1
  ok "update 50"
  $RRDTOOL update ${BUILD}.rrd 1300002001:51.460100:72.901361 1300002041:51.425900:74.332064 1300002081:51.391700:75.761758 1300002121:51.357400:77.190454 1300002161:51.323200:78.618184 1300002201:51.289000:80.044973 1300002241:51.254800:81.470774 1300002281:51.220500:82.895577 1300002321:51.186300:84.319427 1300002361:51.152100:85.742324 || return 1
  ok "update 60"
  $RRDTOOL update ${BUILD}.rrd 1300002401:51.117800:87.164164 1300002441:51.083600:88.585132 1300002481:51.049400:90.005067 1300002521:50.998100:91.423994 1300002561:50.980900:92.841898 1300002601:50.946700:94.258932 1300002641:50.895400:95.674966 1300002681:50.878300:97.090084 1300002721:50.844000:98.504181 1300002761:50.792700:99.917370 || return 1
  ok "update 70"
  $RRDTOOL update ${BUILD}.rrd 1300002801:50.758500:101.329574 1300002841:50.724200:102.740860 1300002881:50.690000:104.151159 1300002921:50.655800:105.560437 1300002961:50.621600:106.968820 1300003001:50.587300:108.376274 1300003041:50.570200:109.782708 1300003081:50.518900:111.188225 1300003121:50.501800:112.592791 1300003161:50.450400:113.996394 || return 1
  ok "update 80"
  $RRDTOOL update ${BUILD}.rrd 1300003201:50.416200:115.398995 1300003241:50.382000:116.800650 1300003281:50.347700:118.201354 1300003321:50.313500:119.601205 1300003361:50.296400:121.000083 1300003401:50.245100:122.398034 1300003441:50.210800:123.795045 1300003481:50.193700:125.191125 1300003521:50.159500:126.586254 1300003561:50.125300:127.980488 || return 1
  ok "update 90"
  $RRDTOOL update ${BUILD}.rrd 1300003601:50.091000:129.373715 1300003641:50.056800:130.766013 1300003681:50.005500:132.157349 1300003721:49.988400:133.547757 1300003761:49.954100:134.937155 1300003801:49.919900:136.325647 1300003841:49.885700:137.713203 1300003881:49.851500:139.099836 1300003921:49.817200:140.485517 1300003961:49.783000:141.870227 || return 1
  ok "update 100"
  $RRDTOOL update ${BUILD}.rrd 1300004001:49.748800:143.254030 1300004041:49.714500:144.636928 1300004081:49.680300:146.018819 1300004121:49.646100:147.399862 1300004161:49.611900:148.779863 1300004201:49.577600:150.158912 1300004241:49.543400:151.536972 1300004281:49.509200:152.914083 1300004321:49.475000:154.290367 1300004361:49.440700:155.665686 || return 1
  ok "update 110"
  $RRDTOOL update ${BUILD}.rrd 1300004401:49.406500:157.040054 1300004441:49.372300:158.413404 1300004481:49.338000:159.785826 1300004521:49.303800:161.157329 1300004561:49.269600:162.527911 1300004601:49.235400:163.897533 1300004641:49.201100:165.266227 1300004681:49.166900:166.634026 1300004721:49.132700:168.000843 1300004761:49.098500:169.366731 || return 1
  ok "update 120"
  $RRDTOOL update ${BUILD}.rrd 1300004801:49.064200:170.731705 1300004841:49.030000:172.095720 1300004881:48.995800:173.458832 1300004921:48.961600:174.820986 1300004961:48.927300:176.182152 1300005001:48.893100:177.542490 1300005041:48.858900:178.901856 1300005081:48.824600:180.260262 1300005121:48.790400:181.617748 1300005161:48.773300:182.974272 || return 1
  ok "update 130"
  $RRDTOOL update ${BUILD}.rrd 1300005201:48.739100:184.329895 1300005241:48.704900:185.684597 1300005281:48.670600:187.038316 1300005321:48.636400:188.391156 1300005361:48.602200:189.743161 1300005401:48.567900:191.094289 1300005441:48.533700:192.444451 1300005481:48.499500:193.793751 1300005521:48.465300:195.142149 1300005561:48.448200:196.489602 || return 1
  ok "update 140"
  $RRDTOOL update ${BUILD}.rrd 1300005601:48.413900:197.836223 1300005641:48.379700:199.181943 1300005681:48.345500:200.526816 1300005721:48.311200:201.870680 1300005761:48.277000:203.213748 1300005801:48.242800:204.555960 1300005841:48.225700:205.897238 1300005881:48.191500:207.237760 1300005921:48.157200:208.577455 1300005961:48.123000:209.916224 || return 1
  ok "update 150"
  $RRDTOOL update ${BUILD}.rrd 1300006001:48.105900:211.254265 1300006041:48.071700:212.591427 1300006081:48.037400:213.927749 1300006121:48.020300:215.263280 1300006161:47.986100:216.598018 1300006201:47.951900:217.931926 1300006241:47.917600:219.265126 1300006281:47.900500:220.597446 1300006321:47.866300:221.929020 1300006361:47.849200:223.259888 || return 1
  ok "update 160"
  $RRDTOOL update ${BUILD}.rrd 1300006401:47.815000:224.589878 1300006441:47.780700:225.919200 1300006481:47.763600:227.247692 1300006521:47.729400:228.575434 1300006561:47.712300:229.902530 1300006601:47.678000:231.228834 1300006641:47.660900:232.554389 1300006681:47.626700:233.879221 1300006721:47.609600:235.203353 1300006761:47.592500:236.526881 || return 1
  ok "update 170"
  $RRDTOOL update ${BUILD}.rrd 1300006801:47.558300:237.849730 1300006841:47.541100:239.171871 1300006881:47.506900:240.493265 1300006921:47.489800:241.814081 1300006961:47.455600:243.134222 1300007001:47.438500:244.453642 1300007041:47.404200:245.772445 1300007081:47.387100:247.090624 1300007121:47.370000:248.408281 1300007161:47.352900:249.725269 || return 1
  ok "update 180"
  $RRDTOOL update ${BUILD}.rrd 1300007201:47.318700:251.041632 1300007241:47.301500:252.357360 1300007281:47.284400:253.672536 1300007321:47.267300:254.987162 1300007361:47.250200:256.301207 1300007401:47.216000:257.614644 1300007441:47.198900:258.927444 1300007481:47.181800:260.239751 1300007521:47.164600:261.551515 1300007561:47.130400:262.862684 || return 1
  ok "update 190"
  $RRDTOOL update ${BUILD}.rrd 1300007601:47.113300:264.173339 1300007641:47.096200:265.483491 1300007681:47.079100:266.793121 1300007721:47.062000:268.102194 1300007761:47.044800:269.410804 1300007801:47.027700:270.718816 1300007841:47.010600:272.026413 1300007881:46.993500:273.333520 1300007921:46.976400:274.640157 1300007961:46.959300:275.946258 || return 1
  ok "update 200"
  $RRDTOOL update ${BUILD}.rrd 1300008001:46.942200:277.251854 1300008041:46.925100:278.557071 1300008081:46.907900:279.861804 1300008121:46.890800:281.166135 1300008161:46.873700:282.469989 1300008201:46.856600:283.773395 1300008241:46.839500:285.076275 1300008281:46.822400:286.378804 1300008321:46.805300:287.680867 1300008361:46.805300:288.982530 || return 1
  ok "update 210"
  $RRDTOOL update ${BUILD}.rrd 1300008401:46.788100:290.283680 1300008441:46.771000:291.584420 1300008481:46.753900:292.884747 1300008521:46.736800:294.184620 1300008561:46.719700:295.484109 1300008601:46.702600:296.783247 1300008641:46.702600:298.081945 1300008681:46.685500:299.380174 1300008721:46.668400:300.677931 1300008761:46.651200:301.975316 || return 1
  ok "update 220"
  $RRDTOOL update ${BUILD}.rrd 1300008801:46.634100:303.272339 1300008841:46.617000:304.568987 1300008881:46.599900:305.865251 1300008921:46.599900:307.161141 1300008961:46.582800:308.456618 1300009001:46.565700:309.751774 1300009041:46.548600:311.046603 1300009081:46.548600:312.341021 1300009121:46.531400:313.635039 1300009161:46.514300:314.928616 || return 1
  ok "update 230"
  $RRDTOOL update ${BUILD}.rrd 1300009201:46.497200:316.221876 1300009241:46.480100:317.514809 1300009281:46.480100:318.807368 1300009321:46.463000:320.099442 1300009361:46.445900:321.391317 1300009401:46.428800:322.682631 1300009441:46.411700:323.973772 1300009481:46.411700:325.264458 1300009521:46.394500:326.554822 1300009561:46.377400:327.844843 || return 1
  ok "update 240"
  $RRDTOOL update ${BUILD}.rrd 1300009601:46.360300:329.134430 1300009641:46.360300:330.423708 1300009681:46.343200:331.712626 1300009721:46.326100:333.001250 1300009761:46.326100:334.289476 1300009801:46.309000:335.577363 1300009841:46.291900:336.864901 1300009881:46.274700:338.152146 1300009921:46.257600:339.438816 1300009961:46.257600:340.725244 || return 1
  ok "update 250"
  $RRDTOOL update ${BUILD}.rrd 1300010001:46.240500:342.011371 1300010041:46.223400:343.297235 1300010081:46.223400:344.582621 1300010121:46.206300:345.867719 1300010161:46.189200:347.152418 1300010201:46.172100:348.436855 1300010241:46.172100:349.720873 1300010281:46.155000:351.004552 1300010321:46.137800:352.287917 1300010361:46.137800:353.570866 || return 1
  ok "update 260"
  $RRDTOOL update ${BUILD}.rrd 1300010401:46.120700:354.853439 1300010441:46.103600:356.135794 1300010481:46.086500:357.417682 1300010521:46.086500:358.699233 1300010561:46.069400:359.980283 1300010601:46.052300:361.261082 1300010641:46.035200:362.541540 1300010681:46.035200:363.821615 1300010721:46.018000:365.101232 1300010761:46.000900:366.380525 || return 1
  ok "update 270"
  $RRDTOOL update ${BUILD}.rrd 1300010801:45.983800:367.659498 1300010841:45.983800:368.938096 1300010881:45.966700:370.216287 1300010921:45.949600:371.494210 1300010961:45.932500:372.771749 1300011001:45.915400:374.049031 1300011041:45.915400:375.325857 1300011081:45.898300:376.602231 1300011121:45.881100:377.878192 1300011161:45.864000:379.153962 || return 1
  ok "update 280"
  $RRDTOOL update ${BUILD}.rrd 1300011201:45.846900:380.429339 1300011241:45.829800:381.704289 1300011281:45.829800:382.978722 1300011321:45.812700:384.252785 1300011361:45.795600:385.526529 1300011401:45.778500:386.799881 1300011441:45.761300:388.072868 1300011481:45.761300:389.345482 1300011521:45.744200:390.617542 1300011561:45.727100:391.889294 || return 1
  ok "update 290"
  $RRDTOOL update ${BUILD}.rrd 1300011601:45.710000:393.160622 1300011641:45.692900:394.431484 1300011681:45.675800:395.701875 1300011721:45.675800:396.971982 1300011761:45.658700:398.241621 1300011801:45.641500:399.510851 1300011841:45.624400:400.779595 1300011881:45.607300:402.047964 1300011921:45.590200:403.315889 1300011961:45.573100:404.583344 || return 1
  ok "update 300"
  $RRDTOOL update ${BUILD}.rrd 1300012001:45.556000:405.850313 1300012041:45.538900:407.116874 1300012081:45.521800:408.382937 1300012121:45.504600:409.648582 1300012161:45.487500:410.913818 1300012201:45.470400:412.178543 1300012241:45.453300:413.442783 1300012281:45.436200:414.706488 1300012321:45.419100:415.969782 1300012361:45.402000:417.232532 || return 1
  ok "update 310"
  $RRDTOOL update ${BUILD}.rrd 1300012401:45.384800:418.494845 1300012441:45.367700:419.756659 1300012481:45.350600:421.018062 1300012521:45.333500:422.278948 1300012561:45.316400:423.539263 1300012601:45.299300:424.799119 1300012641:45.265100:426.058460 1300012681:45.265100:427.317310 1300012721:45.230800:428.575546 1300012761:45.213700:429.833255 || return 1
  ok "update 320"
  $RRDTOOL update ${BUILD}.rrd 1300012801:45.196600:431.090489 1300012841:45.179500:432.347115 1300012881:45.162400:433.603203 1300012921:45.145300:434.858665 1300012961:45.111000:436.113637 1300013001:45.093900:437.367950 1300013041:45.076800:438.621649 1300013081:45.059700:439.874913 1300013121:45.042600:441.127614 1300013161:45.008300:442.379646 || return 1
  ok "update 330"
  $RRDTOOL update ${BUILD}.rrd 1300013201:44.991200:443.631056 1300013241:44.974100:444.881916 1300013281:44.939900:446.132019 1300013321:44.922800:447.381467 1300013361:44.905700:448.630289 1300013401:44.871400:449.878557 1300013441:44.854300:451.126192 1300013481:44.820100:452.373146 1300013521:44.803000:453.619438 1300013561:44.785900:454.865056 || return 1
  ok "update 340"
  $RRDTOOL update ${BUILD}.rrd 1300013601:44.751600:456.110016 1300013641:44.734500:457.354197 1300013681:44.700300:458.597681 1300013721:44.683200:459.840456 1300013761:44.649000:461.082338 1300013801:44.614700:462.323539 1300013841:44.597600:463.563965 1300013881:44.563400:464.803451 1300013921:44.529200:466.042315 1300013961:44.512100:467.280304 || return 1
  ok "update 350"
  $RRDTOOL update ${BUILD}.rrd 1300014001:44.477800:468.517435 1300014041:44.443600:469.753661 1300014081:44.409400:470.988894 1300014121:44.375200:472.223309 1300014161:44.340900:473.456819 1300014201:44.306700:474.689374 1300014241:44.272500:475.920989 1300014281:44.238200:477.151708 1300014321:44.204000:478.381339 1300014361:44.169800:479.609924 || return 1
  ok "update 360"
  $RRDTOOL update ${BUILD}.rrd 1300014401:44.118500:480.837535 1300014441:44.084200:482.063975 1300014481:44.050000:483.289226 1300014521:43.998700:484.513489 1300014561:43.964400:485.736592 1300014601:43.913100:486.958439 1300014641:43.878900:488.178983 1300014681:43.827500:489.398252 1300014721:43.776200:490.616268 1300014761:43.724800:491.832802 || return 1
  ok "update 370"
  $RRDTOOL update ${BUILD}.rrd 1300014801:43.673500:493.047849 1300014841:43.622200:494.261402 1300014881:43.553700:495.473421 1300014921:43.485300:496.683754 1300014961:43.433900:497.892292 1300015001:43.348300:499.098853 1300015041:43.279900:500.303479 1300015081:43.194300:501.505941 1300015121:43.108800:502.705961 1300015161:43.023200:503.903353 || return 1
  ok "update 380"
  $RRDTOOL update ${BUILD}.rrd 1300015201:42.903400:505.097800 1300015241:42.783600:506.289155 1300015281:42.646700:507.476992 1300015321:42.492700:508.660884 1300015361:42.338700:509.840427 1300015401:42.150400:511.015140 1300015441:41.945000:512.184484 1300015481:41.722600:513.347783 1300015521:41.465900:514.504250 1300015561:41.174900:515.653326 || return 1
  ok "update 390"
  $RRDTOOL update ${BUILD}.rrd 1300015601:40.866900:516.794032 1300015641:40.507500:517.925508 1300015681:40.131000:519.046667 1300015721:39.703200:520.156542 || return 1
}

$RRDTOOL create ${BUILD}.rrd --start 1300000000 --step 60s DS:dv:DDERIVE:300:U:U DS:wh:DCOUNTER:300:0:U RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:10:144
report create

do_update
report "update all"

is_cached && exit 0

$BLANK < $BASEDIR/graph2.output > $BASEDIR/graph2.output.out

$RRDTOOL graphv  -s 1300000000 -e 1300015200 --step 600 --imgformat=PNG --add-jsontime - \
	DEF:wh=${BUILD}.rrd:wh:AVERAGE \
	AREA:wh#EA8F007F:'5 Minute Average ':STACK \
	GPRINT:wh:LAST:'Current\:%4.2lf'  \
	GPRINT:wh:AVERAGE:'Average\:%4.2lf'  \
	GPRINT:wh:MAX:'Maximum\:%4.2lf\n' > graph2.output
# | $BLANK | $DIFF9 - $BASEDIR/graph2.output.out

report "graphv"
